package cn.tedu.boot41.controller;

import cn.tedu.boot41.entity.Product;
import cn.tedu.boot41.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;

    @RequestMapping("insert")
    public void insert(@RequestBody Product product) {
        mapper.insert(product);
    }

    @RequestMapping("select")
    public List<Product> select() {
        //SpringMVC框架当发现返回值为集合或自定义对象类型时,
        //会将集合或对象转为JSON格式的字符串,然后再将JSON格式的字符串转成二进制数据进行网络传输
        List<Product> list = mapper.select();
        return list;
    }

    @RequestMapping("delete")
    public void delete(int id) {
        mapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public Product selectById(int id) {
        return mapper.selectById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody Product product) {
        mapper.update(product);
    }
}
